package merchant.space;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;

public class AddItemActivity extends Activity
{
	private Collection collection;
  
	private static final int PHOTO_INFO_DIALOG = 1;
	private static final int SELECT_PHOTO = 200;

	private static final int ERROR = 0;
	private static final int LOADING = 1;
	
	private AlertDialog alertdialog = null;
	private ProgressDialog loginprogressdialog = null;
	
	private CheckBox trade_check,sell_check;
  	private ImageView photo,trade,sell;
  	private Spinner spinner;
  	private RadioButton cam,file;
  	private AddItemActivity context;
  	private Button save;
  	private EditText desc,title;
  	

	private Navigation navigation;	
   
  	private String id;
  	private String filename;
  	private String filepath;
  	private boolean photoon;
  	
//	 private RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
 //  private RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);
  	  
	
	
	 public void onCreate(Bundle savedInstanceState) {
	    	
		  super.onCreate(savedInstanceState);
	      setContentView(R.layout.additem);
	      context=this;
	      
	      MyCollectionsGroupActivity parent = (MyCollectionsGroupActivity)getParent();
	  		navigation = parent.getNavigation();
	  		
	  		photoon=false;
	      
	      save = (Button) findViewById(R.id.save);
	      trade_check = (CheckBox) findViewById(R.id.trade_check);
	      sell_check = (CheckBox) findViewById(R.id.sell_check);
	      trade = (ImageView) findViewById(R.id.tradeable_icon);
	      sell = (ImageView) findViewById(R.id.sellable_icon);
	      photo = (ImageView) findViewById(R.id.photo);
	      photo.setClickable(true);
	      
	      photo.setOnClickListener(imageListener);
	      save.setOnClickListener(saveListener);
	      cam = (RadioButton) findViewById(R.id.cam);
	      file = (RadioButton) findViewById(R.id.file);
	      desc = (EditText) findViewById(R.id.description);
	      title = (EditText) findViewById(R.id.title);
	      
	      cam.setChecked(true);
	     
	      trade_check.setOnClickListener(new OnClickListener() {
			   public void onClick(View v) { 
				   if(trade_check.isChecked())
					   trade.setImageResource(R.drawable.green_hand);
				   else
					   trade.setImageResource(R.drawable.red_hand);
			   }});
	      
	      sell_check.setOnClickListener(new OnClickListener() {
			   public void onClick(View v) { 
				   if(sell_check.isChecked())
					   sell.setImageResource(R.drawable.greendollar);
				   else
					  sell.setImageResource(R.drawable.dollar);
			   }});
	      
	  
   		title.setOnFocusChangeListener(new OnFocusChangeListener()
        {
   		   public void onFocusChange(View v, boolean hasFocus) {
   		   // TODO Auto-generated method stub
   			   if (hasFocus==true)
   			   {
   				   if(title.getText().toString().equals("Title"))
   			   		   title.setText("");
   			   }
	   			else
   			    {
   				 if (title.getText().toString().trim().equals(""))
   					title.setText("Title");
   		   }  }  });
   		
   		desc.setOnFocusChangeListener(new OnFocusChangeListener()
        {
   		   public void onFocusChange(View v, boolean hasFocus) {
   		   // TODO Auto-generated method stub
   			   if (hasFocus==true)
   			   {
   				   if(desc.getText().toString().equals("Description"))
   			   		   desc.setText("");
   			   }
	   			else
   			    {
   				 if (desc.getText().toString().trim().equals(""))
   					desc.setText("Description");
   		   }  }  });
	     
	      
	     
	      collection = DataLink.getMyCollection();
	      Log.d("CREated","sd");

	 
}
	
	
	
	
	private OnClickListener imageListener = new OnClickListener() {
		  
  		public void onClick(View v) {
  				if(cam.isChecked())
  				{
  					useCam();
  				}
  				if(file.isChecked())
  				{
  					pickFile();
  				}
  			}
  	};
  	
  	private OnClickListener saveListener = new OnClickListener() {
		  
  		public void onClick(View v) {
  			
  			
  			boolean trade,sell;
  			if(trade_check.isChecked())
  				trade = true;
  			else
  				trade = false;
  			if(sell_check.isChecked())
  				sell = true;
  			else
  				sell = false;
  			
  			
  			int col_id = collection.getId();
  			int it_id = 50;
  			
  			String tit = title.getText().toString();
  			String dd = desc.getText().toString();
  			
  			if(tit.equals("Title")||tit.trim().equals("")||dd.equals("Description")|dd.trim().equals(""))
  				showDialog(ERROR);
  			else
  			{
  				String photosource,thumbsource;
  				if (photoon)
  				{
  					photosource=filepath;
  					thumbsource=filepath;
  				}
  				else
  				{
  					photosource="/missing_original.png";
  					thumbsource="/missing_thumb.png";
  				}
  				Log.d("bef item","");
	  			Item item = new Item(col_id,it_id,thumbsource,title.getText().toString(),trade,sell,desc.getText().toString(),photosource);
	  			Log.d("after item","");
	  			collection.addItem(item);
	  		//	DataLink.postItem(item,navigation);
	  			finish();
  			}
  			
  		}
  	};
  	
  	
  	private OnClickListener radio_listener = new OnClickListener() {
  	    public void onClick(View v) {
  	        // Perform action on clicks
  	        RadioButton rb = (RadioButton) v;
  	      //  Toast.makeText(this, rb.getText(), Toast.LENGTH_SHORT).show();
  	        
  	    }
  	};
  	
  	
  
	 
	 
	 public void useCam(){
		
		  Uri fileUri;
		  File file;
		// create Intent to take a picture and return control to the calling application
	      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
	      
	   //   String name = String.valueOf(fotocount);
	      filename = DataLink.photoWid();
	      
	      fileUri = CamControl.getOutputMediaFileUri(CamControl.MEDIA_TYPE_IMAGE,filename); // create a file to save the image
	      intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
	      intent.putExtra("myid", "cam"); // set
	      filepath=fileUri.getPath();
	      // start the image capture Intent
	      Activity parent = getParent();
	      Log.d("p->",parent.toString());
	      getParent().startActivityForResult(intent, CamControl.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
	   
	   
	}
	
	public void afterPhoto()
	{
		Log.d("uu","dating photo");
		photo.setImageBitmap(Item.preview(filepath));
		Log.d("cam",filepath);
		photoon=true;
	}
	 
	public void afterPick(String path)
	{
		
		Log.d("After","pic");
		filepath = path;
		photoon=true;
		photo.setImageBitmap(Item.preview(filepath));
		
	}
	 
	
	 public void pickFile()
	 {
		 Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
		 photoPickerIntent.setType("image/*");
		 getParent().startActivityForResult(photoPickerIntent, SELECT_PHOTO);    
	 }

	 
	
	  @Override
	 	protected Dialog onCreateDialog(int id) {
	 		switch (id) {
	 		case ERROR:
			 			
			 			AlertDialog.Builder builder2 = new AlertDialog.Builder(getParent());
			 			builder2.setMessage("Please, fill your data correctly")
			 			       .setCancelable(false)
			 			       .setTitle("Merchant")
			 			       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			 			           public void onClick(DialogInterface dialog, int id) {
			 			             alertdialog.cancel();
			 			        //     refresh();
			 			           }
			 			       });
			 			
			 			alertdialog = builder2.create();
			 			return alertdialog;	
	 			
	 		default:
	 			return null;
	 		}
	 	}
	 
	 
}
